package com.liupf.controller;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.liupf.common.utils.Constants;
import com.liupf.domain.City;
import com.liupf.domain.Tel;

public class CategoryOrderController extends Controller{

	private Log logger = LogFactory.getLog(this.getClass());
	
	public void deliveryList(){
		//操作员自己所属品牌
		String company=(String) getSession().getAttribute("company");
		String cityId=(String) getSession().getAttribute("cityId");
		//int purview=(Integer) getSession().getAttribute("purview");
		String userRole = (String) getSession().getAttribute(Constants.SESSION_USER_ROLE);
		String job_number = getPara("job_number");
		String real_name = getPara("real_name");
		String cell_phone = getPara("cell_phone");
		
		String id = getPara("id");
		String from = "from order_detail a left join driver_member b on a.member_id = b.id left join b_user bu on a.b_user_id = bu.id  where a.order_category_type='DELIVERY' ";
		
		//String from = "from order_detail a where a.order_state=1";
		if (StringUtils.isNotBlank(id)) {
			from += " and a.id = " + id;
		}
		if(StringUtils.isNotBlank(job_number)){
			from += " and bu.job_number = '" + job_number +"'";
		}
		if(StringUtils.isNotBlank(real_name)){
			from += " and a.real_name = '" + real_name +"'";
		}
		if(StringUtils.isNotBlank(cell_phone)){
			from += " and a.cell_phone = '" + cell_phone +"'";
		}
		
		from = Constants.appendSqlForRoleCity(from, userRole, cityId);
		
		from += " order by a.id desc";
		Page<Record> page = Db
				.paginate(
						getParaToInt(0, 1),
						10,
						"select a.*,b.real_name as real_name2,b.user_name,b.cell_phone as cell_phone2,b.sex as sex2 ,b.plage_number as plage_number2,bu.name as driver_name , bu.job_number,bu.phone as driver_phone,a.order_category_type ",
						from);
		setAttr("page", page);
		
		String totalSql = "select count(*) num from order_detail a where a.order_category_type='DELIVERY' ";
		totalSql = Constants.appendSqlForRoleCity(totalSql, userRole, cityId);
		
		Record record = Db.findFirst(totalSql);
		Long totalNum = record.getLong("num");
		
		setAttr("totalNum", totalNum);
				
		render("delivery_list.ftl");
	}
	
	public void unlockList(){
		//操作员自己所属品牌
		String company=(String) getSession().getAttribute("company");
		String cityId=(String) getSession().getAttribute("cityId");
		//int purview=(Integer) getSession().getAttribute("purview");
		String userRole = (String) getSession().getAttribute(Constants.SESSION_USER_ROLE);
		String job_number = getPara("job_number");
		String real_name = getPara("real_name");
		String cell_phone = getPara("cell_phone");
		
		String id = getPara("id");
		String from = "from order_detail a left join driver_member b on a.member_id = b.id left join b_user bu on a.b_user_id = bu.id  where a.order_category_type='UNLOCK' ";
		
		//String from = "from order_detail a where a.order_state=1";
		if (StringUtils.isNotBlank(id)) {
			from += " and a.id = " + id;
		}
		if(StringUtils.isNotBlank(job_number)){
			from += " and bu.job_number = '" + job_number +"'";
		}
		if(StringUtils.isNotBlank(real_name)){
			from += " and a.real_name = '" + real_name +"'";
		}
		if(StringUtils.isNotBlank(cell_phone)){
			from += " and a.cell_phone = '" + cell_phone +"'";
		}
		
		from = Constants.appendSqlForRoleCity(from, userRole, cityId);
		
		from += " order by a.id desc";
		Page<Record> page = Db
				.paginate(
						getParaToInt(0, 1),
						10,
						"select a.*,b.real_name as real_name2,b.user_name,b.cell_phone as cell_phone2,b.sex as sex2 ,b.plage_number as plage_number2,bu.name as driver_name , bu.job_number,bu.phone as driver_phone,a.order_category_type ",
						from);
		setAttr("page", page);
		
		String totalSql = "select count(*) num from order_detail a where a.order_category_type='UNLOCK' ";
		totalSql = Constants.appendSqlForRoleCity(totalSql, userRole, cityId);
		
		Record record = Db.findFirst(totalSql);
		Long totalNum = record.getLong("num");
		
		setAttr("totalNum", totalNum);
				
		render("unlock_list.ftl");
	}
	
	public void airportList(){
		//操作员自己所属品牌
		String company=(String) getSession().getAttribute("company");
		//int purview=(Integer) getSession().getAttribute("purview");
		String userRole = (String) getSession().getAttribute(Constants.SESSION_USER_ROLE);
		String job_number = getPara("job_number");
		String real_name = getPara("real_name");
		String cell_phone = getPara("cell_phone");
		
		String id = getPara("id");
		String from = "from order_detail a left join driver_member b on a.member_id = b.id left join b_user bu on a.b_user_id = bu.id  where a.order_category_type='AIRPORT' ";
		
		//String from = "from order_detail a where a.order_state=1";
		if (StringUtils.isNotBlank(id)) {
			from += " and a.id = " + id;
		}
		if(StringUtils.isNotBlank(job_number)){
			from += " and bu.job_number = '" + job_number +"'";
		}
		if(StringUtils.isNotBlank(real_name)){
			from += " and a.real_name = '" + real_name +"'";
		}
		if(StringUtils.isNotBlank(cell_phone)){
			from += " and a.cell_phone = '" + cell_phone +"'";
		}
		
		from = Constants.appendSqlForRoleCom(from, userRole, company);
		
		from += " order by a.id desc";
		Page<Record> page = Db
				.paginate(
						getParaToInt(0, 1),
						10,
						"select a.*,b.real_name as real_name2,b.user_name,b.cell_phone as cell_phone2,b.sex as sex2 ,b.plage_number as plage_number2,bu.name as driver_name , bu.job_number,bu.phone as driver_phone,a.order_category_type ",
						from);
		setAttr("page", page);
				
		render("airport_list.ftl");
	}
	
	public void stationList(){
		//操作员自己所属品牌
		String company=(String) getSession().getAttribute("company");
		//int purview=(Integer) getSession().getAttribute("purview");
		String userRole = (String) getSession().getAttribute(Constants.SESSION_USER_ROLE);
		String job_number = getPara("job_number");
		String real_name = getPara("real_name");
		String cell_phone = getPara("cell_phone");
		
		String id = getPara("id");
		String from = "from order_detail a left join driver_member b on a.member_id = b.id left join b_user bu on a.b_user_id = bu.id  where a.order_category_type='STATION' ";
		
		//String from = "from order_detail a where a.order_state=1";
		if (StringUtils.isNotBlank(id)) {
			from += " and a.id = " + id;
		}
		if(StringUtils.isNotBlank(job_number)){
			from += " and bu.job_number = '" + job_number +"'";
		}
		if(StringUtils.isNotBlank(real_name)){
			from += " and a.real_name = '" + real_name +"'";
		}
		if(StringUtils.isNotBlank(cell_phone)){
			from += " and a.cell_phone = '" + cell_phone +"'";
		}
		
		from = Constants.appendSqlForRoleCom(from, userRole, company);
		
		from += " order by a.id desc";
		Page<Record> page = Db
				.paginate(
						getParaToInt(0, 1),
						10,
						"select a.*,b.real_name as real_name2,b.user_name,b.cell_phone as cell_phone2,b.sex as sex2 ,b.plage_number as plage_number2,bu.name as driver_name , bu.job_number,bu.phone as driver_phone,a.order_category_type ",
						from);
		setAttr("page", page);
				
		render("station_list.ftl");
	}
	
	public void cleanserverList(){
		//操作员自己所属品牌
		String company=(String) getSession().getAttribute("company");
		//int purview=(Integer) getSession().getAttribute("purview");
		String userRole = (String) getSession().getAttribute(Constants.SESSION_USER_ROLE);
		String job_number = getPara("job_number");
		String real_name = getPara("real_name");
		String cell_phone = getPara("cell_phone");
		
		String id = getPara("id");
		String from = "from order_detail a left join driver_member b on a.member_id = b.id left join b_user bu on a.b_user_id = bu.id  where a.order_category_type='CLEANSERVER' ";
		
		//String from = "from order_detail a where a.order_state=1";
		if (StringUtils.isNotBlank(id)) {
			from += " and a.id = " + id;
		}
		if(StringUtils.isNotBlank(job_number)){
			from += " and bu.job_number = '" + job_number +"'";
		}
		if(StringUtils.isNotBlank(real_name)){
			from += " and a.real_name = '" + real_name +"'";
		}
		if(StringUtils.isNotBlank(cell_phone)){
			from += " and a.cell_phone = '" + cell_phone +"'";
		}
		
		from = Constants.appendSqlForRoleCom(from, userRole, company);
		
		from += " order by a.id desc";
		Page<Record> page = Db
				.paginate(
						getParaToInt(0, 1),
						10,
						"select a.*,b.real_name as real_name2,b.user_name,b.cell_phone as cell_phone2,b.sex as sex2 ,b.plage_number as plage_number2,bu.name as driver_name , bu.job_number,bu.phone as driver_phone,a.order_category_type ",
						from);
		setAttr("page", page);
				
		render("cleanserver_list.ftl");
	}
	
	public void babysitterList(){
		//操作员自己所属品牌
		String company=(String) getSession().getAttribute("company");
		String userRole = (String) getSession().getAttribute(Constants.SESSION_USER_ROLE);
		String job_number = getPara("job_number");
		String real_name = getPara("real_name");
		String cell_phone = getPara("cell_phone");
		
		String id = getPara("id");
		String from = "from order_detail a left join driver_member b on a.member_id = b.id left join b_user bu on a.b_user_id = bu.id  where a.order_category_type='BABYSITTER' ";
		
		//String from = "from order_detail a where a.order_state=1";
		if (StringUtils.isNotBlank(id)) {
			from += " and a.id = " + id;
		}
		if(StringUtils.isNotBlank(job_number)){
			from += " and bu.job_number = '" + job_number +"'";
		}
		if(StringUtils.isNotBlank(real_name)){
			from += " and a.real_name = '" + real_name +"'";
		}
		if(StringUtils.isNotBlank(cell_phone)){
			from += " and a.cell_phone = '" + cell_phone +"'";
		}
		
		from = Constants.appendSqlForRoleCom(from, userRole, company);
		
		from += " order by a.id desc";
		Page<Record> page = Db
				.paginate(
						getParaToInt(0, 1),
						10,
						"select a.*,b.real_name as real_name2,b.user_name,b.cell_phone as cell_phone2,b.sex as sex2 ,b.plage_number as plage_number2,bu.name as driver_name , bu.job_number,bu.phone as driver_phone,a.order_category_type ",
						from);
		setAttr("page", page);
				
		render("babysitter_list.ftl");
	}
	//违章处理
	public void breakruleList(){
		//操作员自己所属品牌
		String company=(String) getSession().getAttribute("company");
		String userRole = (String) getSession().getAttribute(Constants.SESSION_USER_ROLE);
		String job_number = getPara("job_number");
		String real_name = getPara("real_name");
		String cell_phone = getPara("cell_phone");
		
		String id = getPara("id");
		String from = "from order_detail a left join driver_member b on a.member_id = b.id left join b_user bu on a.b_user_id = bu.id  where a.order_category_type='BREAKRULE' ";
		
		//String from = "from order_detail a where a.order_state=1";
		if (StringUtils.isNotBlank(id)) {
			from += " and a.id = " + id;
		}
		if(StringUtils.isNotBlank(job_number)){
			from += " and bu.job_number = '" + job_number +"'";
		}
		if(StringUtils.isNotBlank(real_name)){
			from += " and a.real_name = '" + real_name +"'";
		}
		if(StringUtils.isNotBlank(cell_phone)){
			from += " and a.cell_phone = '" + cell_phone +"'";
		}
		
		from = Constants.appendSqlForRoleCom(from, userRole, company);
		
		from += " order by a.id desc";
		Page<Record> page = Db
				.paginate(
						getParaToInt(0, 1),
						10,
						"select a.*,b.real_name as real_name2,b.user_name,b.cell_phone as cell_phone2,b.sex as sex2 ,b.plage_number as plage_number2,bu.name as driver_name , bu.job_number,bu.phone as driver_phone,a.order_category_type ",
						from);
		setAttr("page", page);
				
		render("breakrule_list.ftl");
	}
	//车辆检修
	public void caroverhaulList(){
		//操作员自己所属品牌
		String company=(String) getSession().getAttribute("company");
		String userRole = (String) getSession().getAttribute(Constants.SESSION_USER_ROLE);
		String job_number = getPara("job_number");
		String real_name = getPara("real_name");
		String cell_phone = getPara("cell_phone");
		
		String id = getPara("id");
		String from = "from order_detail a left join driver_member b on a.member_id = b.id left join b_user bu on a.b_user_id = bu.id  where a.order_category_type='CAROVERHAUL' ";
		
		//String from = "from order_detail a where a.order_state=1";
		if (StringUtils.isNotBlank(id)) {
			from += " and a.id = " + id;
		}
		if(StringUtils.isNotBlank(job_number)){
			from += " and bu.job_number = '" + job_number +"'";
		}
		if(StringUtils.isNotBlank(real_name)){
			from += " and a.real_name = '" + real_name +"'";
		}
		if(StringUtils.isNotBlank(cell_phone)){
			from += " and a.cell_phone = '" + cell_phone +"'";
		}
		
		from = Constants.appendSqlForRoleCom(from, userRole, company);
		
		from += " order by a.id desc";
		Page<Record> page = Db
				.paginate(
						getParaToInt(0, 1),
						10,
						"select a.*,b.real_name as real_name2,b.user_name,b.cell_phone as cell_phone2,b.sex as sex2 ,b.plage_number as plage_number2,bu.name as driver_name , bu.job_number,bu.phone as driver_phone,a.order_category_type ",
						from);
		setAttr("page", page);
				
		render("caroverhaul_list.ftl");
	}
	
	//车辆检修
	public void trunkList(){
		//操作员自己所属品牌
		String company=(String) getSession().getAttribute("company");
		String userRole = (String) getSession().getAttribute(Constants.SESSION_USER_ROLE);
		String job_number = getPara("job_number");
		String real_name = getPara("real_name");
		String cell_phone = getPara("cell_phone");
		
		String id = getPara("id");
		String from = "from order_detail a left join driver_member b on a.member_id = b.id left join b_user bu on a.b_user_id = bu.id  where a.order_category_type='TRUNK' ";
		
		//String from = "from order_detail a where a.order_state=1";
		if (StringUtils.isNotBlank(id)) {
			from += " and a.id = " + id;
		}
		if(StringUtils.isNotBlank(job_number)){
			from += " and bu.job_number = '" + job_number +"'";
		}
		if(StringUtils.isNotBlank(real_name)){
			from += " and a.real_name = '" + real_name +"'";
		}
		if(StringUtils.isNotBlank(cell_phone)){
			from += " and a.cell_phone = '" + cell_phone +"'";
		}
		
		from = Constants.appendSqlForRoleCom(from, userRole, company);
		
		from += " order by a.id desc";
		Page<Record> page = Db
				.paginate(
						getParaToInt(0, 1),
						10,
						"select a.*,b.real_name as real_name2,b.user_name,b.cell_phone as cell_phone2,b.sex as sex2 ,b.plage_number as plage_number2,bu.name as driver_name , bu.job_number,bu.phone as driver_phone,a.order_category_type ",
						from);
		setAttr("page", page);
				
		render("trunk_list.ftl");
	}
	
	//猎头
	public void headhuntingList(){
		//操作员自己所属品牌
		String company=(String) getSession().getAttribute("company");
		String userRole = (String) getSession().getAttribute(Constants.SESSION_USER_ROLE);
		String job_number = getPara("job_number");
		String real_name = getPara("real_name");
		String cell_phone = getPara("cell_phone");
		
		String id = getPara("id");
		String from = "from order_detail a left join driver_member b on a.member_id = b.id left join b_user bu on a.b_user_id = bu.id  where a.order_category_type='HEADHUNTING' ";
		
		//String from = "from order_detail a where a.order_state=1";
		if (StringUtils.isNotBlank(id)) {
			from += " and a.id = " + id;
		}
		if(StringUtils.isNotBlank(job_number)){
			from += " and bu.job_number = '" + job_number +"'";
		}
		if(StringUtils.isNotBlank(real_name)){
			from += " and a.real_name = '" + real_name +"'";
		}
		if(StringUtils.isNotBlank(cell_phone)){
			from += " and a.cell_phone = '" + cell_phone +"'";
		}
		
		from = Constants.appendSqlForRoleCom(from, userRole, company);
		
		from += " order by a.id desc";
		Page<Record> page = Db
				.paginate(
						getParaToInt(0, 1),
						10,
						"select a.*,b.real_name as real_name2,b.user_name,b.cell_phone as cell_phone2,b.sex as sex2 ,b.plage_number as plage_number2,bu.name as driver_name , bu.job_number,bu.phone as driver_phone,a.order_category_type ",
						from);
		setAttr("page", page);
				
		render("headhunting_list.ftl");
	}
	
	public void parkList(){
		//操作员自己所属品牌
		String company=(String) getSession().getAttribute("company");
		String cityId=(String) getSession().getAttribute("cityId");
		String userRole = (String) getSession().getAttribute(Constants.SESSION_USER_ROLE);
		String job_number = getPara("job_number");
		String real_name = getPara("real_name");
		String cell_phone = getPara("cell_phone");
		
		String id = getPara("id");
		String from = "from order_detail a left join driver_member b on a.member_id = b.id left join b_user bu on a.b_user_id = bu.id  where a.order_category_type='PARK' ";
		
		//String from = "from order_detail a where a.order_state=1";
		if (StringUtils.isNotBlank(id)) {
			from += " and a.id = " + id;
		}
		if(StringUtils.isNotBlank(job_number)){
			from += " and bu.job_number = '" + job_number +"'";
		}
		if(StringUtils.isNotBlank(real_name)){
			from += " and a.real_name = '" + real_name +"'";
		}
		if(StringUtils.isNotBlank(cell_phone)){
			from += " and a.cell_phone = '" + cell_phone +"'";
		}
		
		from = Constants.appendSqlForRoleCity(from, userRole, cityId);
		
		from += " order by a.id desc";
		Page<Record> page = Db
				.paginate(
						getParaToInt(0, 1),
						10,
						"select a.*,b.real_name as real_name2,b.user_name,b.cell_phone as cell_phone2,b.sex as sex2 ,b.plage_number as plage_number2,bu.name as driver_name , bu.job_number,bu.phone as driver_phone,a.order_category_type ",
						from);
		setAttr("page", page);
		
		String totalSql = "select count(*) num from order_detail a where a.order_category_type='PARK' ";
		totalSql = Constants.appendSqlForRoleCity(totalSql, userRole, cityId);
		
		Record record = Db.findFirst(totalSql);
		Long totalNum = record.getLong("num");
		
		setAttr("totalNum", totalNum);
				
		render("park_list.ftl");
	}
	
	public void deliveryPrice(){
		
		String company=(String) getSession().getAttribute("syscity");
		String userRole = (String) getSession().getAttribute(Constants.SESSION_USER_ROLE);
		
		String from="from tel_company";
		if(!"admin".equals(userRole)){
			from=from +" where  city='"+company+"'";
		}
		Page<Tel> page = Tel.dao.paginate(getParaToInt(0, 1), 10,
				"select * ",
				from+" order by id desc");
		setAttr("page", page);
		
		render("delivery_price.ftl");
	}
	
	public void editprice(){
		int id=getParaToInt();
		Tel tel=Tel.dao.findById(id);
		setAttr("city", tel);
		render("delivery_edit.ftl");
	}
	
	public void eidtDelivery(){
		Tel tel=getModel(Tel.class);
		tel.update();
		redirect("/categoryorder/deliveryPrice");
	}
	
}
